Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : Vrai-Prix8 * File : src/app/api/listings/[uid]/ai-cost-analysis/route.ts9 * Module : app10 * Repository : https://git.spboucher.ai/vrai-prix11 * Created : 2026-09-0812 * Last Modified : 2026-09-0813 *14 * Author : Simon-Pierre Boucher15 * Position : Professor of Finance16 * Institution : Université du Québec en Outaouais (UQO)17 * Organization : SP Boucher18 * Website : https://spboucher.ai19 * Git : https://git.spboucher.ai20 *21 * Preferred Contact: contact@spboucher.ai22 * Development : dev@spboucher.ai23 * Support : support@spboucher.ai24 * Security/Admin : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142// Vrai-Prix — analyse multimodale d'une annonce : démarrage (POST, sur demande seulement) et suivi/lecture (GET).43import { NextRequest, NextResponse } from "next/server";44import { AnalysisError, getById, getLatest, startAnalysis } from "@/lib/cost/ai/service";45import { clientKey, rateLimit } from "@/lib/cost/cache";4647export const runtime = "nodejs";48export const maxDuration = 60;4950export async function POST(req: NextRequest, { params }: { params: Promise<{ uid: string }> }) {51 const { uid } = await params;52 if (!rateLimit(`ai:${clientKey(req)}`, 3, 10 * 60_000)) return NextResponse.json({ error: "Trop d'analyses lancées — réessayez dans quelques minutes.", code: "rate_limited" }, { status: 429 });53 try {54 const r = await startAnalysis(decodeURIComponent(uid), { force: false });55 return NextResponse.json(r, { status: r.reused ? 200 : 202 });56 } catch (e) {57 return errorResponse(e);58 }59}6061export async function GET(req: NextRequest, { params }: { params: Promise<{ uid: string }> }) {62 const { uid } = await params;63 const id = req.nextUrl.searchParams.get("id");64 try {65 const v = id ? getById(id) : getLatest(decodeURIComponent(uid));66 if (!v) return NextResponse.json({ status: "none" }, { headers: { "Cache-Control": "no-store" } });67 if (v.listingUid !== decodeURIComponent(uid)) return NextResponse.json({ error: "analyse d'une autre annonce" }, { status: 400 });68 // en cours : réponse légère pour le polling69 if (v.status !== "completed" && v.status !== "failed") return NextResponse.json({ id: v.id, status: v.status, stage: v.stage, version: v.version, imageCount: v.imageCount, createdAt: v.createdAt }, { headers: { "Cache-Control": "no-store" } });70 return NextResponse.json(v, { headers: { "Cache-Control": "no-store" } });71 } catch (e) {72 return errorResponse(e);73 }74}7576function errorResponse(e: unknown) {77 if (e instanceof AnalysisError) return NextResponse.json({ error: e.message, code: e.code }, { status: e.status });78 return NextResponse.json({ error: (e as Error).message ?? "erreur", code: "error" }, { status: 500 });79}80